<?php

declare(strict_types=1);

namespace app\common\service\im;

use app\common\traits\ServiceTrait;
use app\common\dao\im\GroupDao;
use app\common\service\hrm\CompanyService;
use app\common\service\hrm\DeptService;
use app\common\service\sys\UserService;
use app\common\dao\im\GroupUserDao;
use app\common\dao\im\ChatlogDao;

/**
 * 聊天-群
 * @author:kaige
 * @update:2022-06-12
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class GroupService
{
  protected $dao = null;

  public function __construct()
  {
    $this->dao = new GroupDao();
  }
  use ServiceTrait;

  /**
   * 重写 表格过滤条件
   * @param $post
   * @return array
   */
  public function _list_where($post)
  {
    $where = [];
    if (isset($post['company_id']) && $post['company_id'] != "") {
      $where[] = ['company_id', '=', $post['company_id']];
    }
    if (isset($post['dept_id']) && $post['dept_id'] != "") {
      $where[] = ['dept_id', '=', $post['dept_id']];
    }
    if (isset($post['type']) && $post['type'] != "") {
      $where[] = ['type', '=', $post['type']];
    }
    if (isset($post['status']) && $post['status'] != "") {
      $where[] = ['status', '=', $post['status']];
    }
    if (isset($post['keywords']) && !empty($post['keywords'])) {
      $where[] = ['name', 'like', '%' . $post['keywords'] . '%'];
    }

    $result['where'] = $where;
    $result['field'] = [];
    $result['order'] = ['id desc'];
    return $result;
  }

  /**
   * 重写 数据格式化
   */
  public function _list_format($list)
  {
    foreach ($list as $k => $v) {
      if (isset($v['chat_head'])) {
        $list[$k]['chat_head'] = _img($v['chat_head']);
      }
      if (isset($v['dept_id'])) {
        $list[$k]['dept_id_name'] = DeptService::getNameById($v['dept_id']);
      }
      if (isset($v['company_id'])) {
        $list[$k]['company_name'] = CompanyService::getNameById($v['company_id']);
      }
    }
    return $list;
  }

  /**
   * 添加、编辑数据保存
   * @param $data
   * @return array
   */
  public function saveData($data)
  {
    url_decode($data); // 前端已 encodeURIComponent 编码（最深二维数组） 后端需要解码
    $this->formatData($data);
    return $this->saveForm($data);
  }

  /**
   * 格式化存储的数据--存储
   * @return void
   */
  protected function formatData(&$data)
  {
    $data['status'] = checkon('status', $data);
    if (isset($data['id']) && !empty($data['id'])) {
    } else {
      $id = $data['uid'];
      $uinfo = (new UserService())->getInfoById($id);
      if ($uinfo) {
        $data['create_id'] = $id;
        $data['create_name'] = $uinfo['real_name'];
        $data['uid'] = $id;
        $data['nickname'] = $uinfo['real_name'];
      }
    }
  }

  /**
   * 获取群组列表--API用
   */
  public function getGroupList($data = [], $userinfo)
  {
    $where = [];
    $where[] = ['status', '=', 1];
    $where[] = ['type', 'IN', [1, 2]];
    if (isset($data['keywords']) && !empty($data['keywords'])) {
      $where[] = ['name', 'like', '%' . $data['keywords'] . '%'];
    }
    $list = (new GroupUserDao())->getListByUserId($userinfo['id']);

    if ($list) {
      foreach ($list as $k => $v) {
        $list1 = $this->dao->getInfoByWhere(['id' => $v['group_id']]);
        $list[$k]['chat_head'] = _img($list1['chat_head']);
        $list[$k]['type'] = $list1['type'];
      }
    }


    return $list;
  }

  /**
   * 获取群组列表--Szim
   */
  public function getGroupListSzim()
  {
    $list = $this->dao->getListAll([['status', '=', 1], ['type', 'IN', [1, 2]]], ['sort' => 'asc'], ['id,name,chat_head']);
    foreach ($list as $k => $v) {
      if (isset($v['chat_head'])) {
        $list[$k]['avatar'] = _img($v['chat_head']);
      }
    }
    return $list;
  }
  //退出群
  public function quitgroup($data)
  {
    $result = [
      'status' => false,
      'data'   => '',
      'msg'    => ''
    ];
    (new GroupUserDao())->deleteByWhere(['uid' => $data['uid'], 'group_id' => $data['group_id']]);
    (new ChatlogDao())->deleteByWhere(['send_id' => $data['uid'], 'group_id' => $data['group_id']]);
    $result = (new ChatlogDao())->getListAll(['group_id' => $data['group_id']]);
    foreach($result as $k=>$v){
      $group_uid = explode(",",$v['group_uid']);
      unset($group_uid[array_search($data['uid'],$group_uid)]);
      $data1 = [];
      $data1['group_uid' ] =  implode(',', $group_uid);
      (new ChatlogDao())->updateByWhere(['group_id' => $data['group_id']], $data1);
    }
    return _ok('退出群成功!');
  }
  //解散群
  public function dissolvegroup($data)
  {
    $result = [
      'status' => false,
      'data'   => '',
      'msg'    => ''
    ];
    (new GroupUserDao())->deleteByWhere(['group_id' => $data['group_id']]);
    (new ChatlogDao())->deleteByWhere(['group_id' => $data['group_id']]);
    $this->dao->deleteByWhere(['uid' => $data['uid'],'id' => $data['group_id']]);
    return _ok('群解散成功!');
  }
}
